package com.lonely.fx.list_view;

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.text.DateFormat;
import java.util.HashMap;

/**
 * @date: 2022-07-29 20:28
 * @author: ztkj-hzb
 * @description: 拖拽滑动 演示
 **/
public class DragSlideSettingMain extends Application {

    int selectIndex = 0;
    String selectItem = "";

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        ObservableList<String> list = FXCollections.observableArrayList();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");

        ListView<String> listView = new ListView<>(list);
        listView.setPrefSize(300, 300);

        AnchorPane.setLeftAnchor(listView, 100.0);
        AnchorPane.setTopAnchor(listView, 100.0);
        anchorPane.getChildren().addAll(listView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param) {
                ListCell<String> listCell = new ListCell<String>() {

                    /**
                     *
                     */
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            return;
                        }
                        this.setGraphic(new Label(item));
                    }

                    /**
                     * 记录当前下标
                     */
                    @Override
                    public void updateIndex(int i) {
                        super.updateIndex(i);

                        if (i >= param.getItems().size()) {
                            this.setUserData(param.getItems().size()-1);
                        } else {
                            this.setUserData(i);
                        }
                    }
                };


                listCell.setOnDragDetected(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        Dragboard dragboard = listCell.startDragAndDrop(TransferMode.COPY);

                        ClipboardContent clipboardContent = new ClipboardContent();
                        clipboardContent.putString(selectItem);
                        dragboard.setContent(clipboardContent);


                        Text text = new Text(selectItem);
                        System.out.println(selectItem);
                        WritableImage writableImage = new WritableImage((int) listCell.getWidth(),(int) listCell.getHeight());
                        text.snapshot(null,writableImage);

                        dragboard.setDragView(writableImage);

                    }
                });

                //当前行内拖拽
                listCell.setOnDragOver(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        event.acceptTransferModes(event.getTransferMode());
                    }
                });

                //进入当前行
                listCell.setOnDragEntered(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        int index = (int) listCell.getUserData();
                        param.getFocusModel().focus(index);
                    }
                });

                //释放拖拽
                listCell.setOnDragDropped(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {

                        String oldItem = selectItem;
                        int oldIndex = selectIndex;

                        int index = (int) listCell.getUserData();
                        String newItem = param.getItems().get(index);

                        param.getItems().set(oldIndex,newItem);
                        param.getItems().set(index,oldItem);

                        System.out.println("selectItem: " + oldItem + "--" + "seleteIndex " + "--" + oldIndex + "--" + "index" + index);
                    }
                });


                return listCell;
            }
        });


        listView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                selectIndex = newValue.intValue();
            }
        });

        listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                selectItem = newValue;
            }
        });

    }
}
